/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package org.sbbs.base.async.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
import org.sbbs.demo.async.future.MedalInfo;
import org.sbbs.demo.async.future.MedalService;
import org.sbbs.demo.async.future.UserInfo;
import org.sbbs.demo.async.future.UserInfoService;

/**
 *
 * @author jenkins
 */
public class FutureTest_1 {
    @Test
    public void mainTest() throws  TimeoutException, InterruptedException, ExecutionException{
        try {
            UserInfoService userInfoService = new UserInfoService();
            MedalService medalService = new MedalService();
            long userId =666L;
            long startTime = System.currentTimeMillis();
            
            //调用用户服务获取用户基本信息
            CompletableFuture<UserInfo> completableUserInfoFuture = CompletableFuture.supplyAsync(
                    () -> userInfoService.getUserInfo(userId)
            );
            
            Thread.sleep(300); //模拟主线程其它操作耗时
            
            CompletableFuture<MedalInfo> completableMedalInfoFuture = CompletableFuture.supplyAsync(
                    () -> medalService.getMedalInfo(userId));
            
            UserInfo userInfo = completableUserInfoFuture.get(2,TimeUnit.SECONDS);//获取个人信息结果
            MedalInfo medalInfo = completableMedalInfoFuture.get();//获取勋章信息结果
            System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
        } catch (InterruptedException ex) {
            Logger.getLogger(FutureTest_1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
